<!-- 屈光发育档案/检查信息管理 -->
<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
      <el-button type="primary" icon="el-icon-delete" @click="searchReset">清空</el-button>
      <el-button v-if="permissions.pvm_arcOptometry_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle('edit')">新增</el-button>
      <el-button @click="printInfo">导出成Excel</el-button>
    </el-button-group>
    <el-form inline ref="searchForm" :model="dataForm" @keyup.enter.native="searchHandle()">
      <el-row>
        <el-col :span="12">
          <el-form-item label="检查时间">
            <el-row>
              <el-col :span="11">
                <el-date-picker v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"
                  :picker-options="pickerOptions"></el-date-picker>
              </el-col>
              <el-col class="line" :span="2" style="text-align: center;">-</el-col>
              <el-col :span="11">
                <el-date-picker v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"
                  :picker-options="pickerOptions"></el-date-picker>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="就诊卡号">
            <el-input v-model="dataForm.cardNum"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="姓名">
            <el-input v-model="dataForm.personName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="学校类型">
            <el-select v-model="dataForm.schoolType" @change="schoolTypeChange" clearable>
              <el-option v-for="item in schoolTypeOption" :value="item.value" :label="item.label" :key="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="学校名称">
            <el-select v-model="dataForm.schoolId" @change="schoolChange" filterable clearable>
              <el-option v-for="item in schoolOption" :value="item.id" :label="item.schoolName" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="年级">
            <el-select v-model="dataForm.gradeId" @change="getClassOption" filterable clearable>
              <el-option v-for="item in gradeOption" :value="item.id" :label="item.gradeName" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="班级">
            <el-select filterable default-first-option clearable v-model="dataForm.classId">
              <el-option v-for="item in classOptions" :value="item.id" :label="item.className" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="验光医生">
            <el-select v-model="dataForm.checkDoctor" filterable clearable>
              <el-option v-for="item in userOption" :value="item.id" :label="item.realName" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="唯一编码">
            <el-input v-model="dataForm.uniqueCode"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading">
        <el-table-column type="index" label="序号" ></el-table-column>
        <el-table-column prop="checkDate" label="检查时间"></el-table-column>
        <el-table-column prop="personName" label="姓名"></el-table-column>
        <el-table-column prop="birthDay" label="出生日期"></el-table-column>
        <el-table-column prop="cardNum" label="就诊卡号"></el-table-column>
        <el-table-column prop="mainDoctorName" label="主检医生"></el-table-column>
        <el-table-column prop="checkDoctorName" label="验光医生"></el-table-column>
        <el-table-column label="操作" width="210">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="addOrUpdateHandle('check',scope.row.id,scope.row.docId)">详情</el-button>
            <el-button v-if="permissions.pvm_arcOptometry_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle('edit',scope.row.id,scope.row.docId)">修改</el-button>
            <el-button v-if="permissions.pvm_arcOptometry_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    <order-info v-if="infoVisible" ref="infoDialog"></order-info>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { fetchPvmArcOptometryList, delPvmArcOptometryObj, getOptometryListExport } from '@/api/pvm/arcoptometry'
  import { remote } from '@/api/admin/dict'
  import { filterParams } from "@/util/tools";
  import { getSchoolOptions, getSchoolListBySchoolType } from '@/api/pvm/school'
  import { getGradeBySchoolId } from '@/api/pvm/grade'
  import { getClassOptions } from '@/api/pvm/school'
  import { getUserListByMenu, getUserByDeptIdAndJobType } from '@/api/admin/user'

  import TableForm from './form'

  export default {
    data() {
      return {
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now()
          }
        },
        dataForm: {
          key: '',
          schoolName: '',
          gradeName: ''
        },
        infoVisible: false,
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        schoolTypeOption: [],
        schoolOption: [],
        gradeOption: [],
        classOptions: [],
        userOption: [],
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      TableForm
    },
    created() {
      remote('school_type').then(response => {
        this.schoolTypeOption = response.data.data
      })
      this.getUserList()
      this.getDataList()
      this.getAllSchool()
    },
    computed: {
      ...mapGetters(["permissions",'bizData', "userInfo"]),
      tableHeight: function(){
        return window.innerHeight - 350
      }
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        let params1 = filterParams(this.dataForm);
        let params2 = {
          current: this.pageIndex,
          size: this.pageSize
        };
        fetchPvmArcOptometryList(Object.assign(params2, params1)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(type,id, docId) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(type, id, '三维检查', docId)
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delPvmArcOptometryObj(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      searchHandle() {
        this.pageIndex = 1;
        this.getDataList()
      },
      // 搜索清空
      searchReset() {
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
        this.getDataList(this.page)
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList(this.page)
      },
      //  查看配镜单详情
      checkDetailByOrder(order) {
        this.infoVisible = true;
        this.$nextTick(() => {
          this.$refs.infoDialog.init('', order, '配镜单详情');
        });
      },
      getAllSchool() {
        getSchoolOptions(this.dataForm.schoolType).then(response => {
          if(response.data.code == '0'){
            this.schoolOption = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      schoolTypeChange() {
        this.schoolOption = [],
        this.gradeOption = [],
        this.classOptions = [],
        this.$set(this.dataForm, 'schoolId', '')
        this.$set(this.dataForm, 'gradeId', '')
        this.$set(this.dataForm, 'classId', '')
        if (this.dataForm.schoolType) {
          getSchoolListBySchoolType(this.dataForm.schoolType).then(response => {
            if(response.data.code == '0'){
              this.schoolOption = response.data.data
            }else{
              this.$message.error(response.data.msg)
            }

          })
        } else {
          getSchoolOptions(this.dataForm.schoolType).then(response => {
            if(response.data.code == '0'){
              this.schoolOption = response.data.data
            }else{
              this.$message.error(response.data.msg)
            }
          })
        }
      },
      schoolChange(id) {
        this.gradeOption = [],
          this.classOptions = [],
          this.$set(this.dataForm, 'gradeId', '')
        this.$set(this.dataForm, 'classId', '')
        if (id) {
          getGradeBySchoolId(id).then(response => {
            if(response.data.code == '0'){
              this.gradeOption = response.data.data
            }else{
              this.$message.error(response.data.msg)
            }
          })
        }
      },
      // 获取班级列表
      getClassOption(gradeId) {
        this.classOptions = []
        this.$set(this.dataForm, 'classId', '')
        getClassOptions(this.dataForm.schoolId, gradeId).then(response => {
          if(response.data.code == '0'){
            this.classOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
        //  根据key值获取url
        getPathByKey(key) {
            let obj = {}
            obj = this.bizData.personTypeData.find(item => {
                if (item.key == key) {
                    return item
                }
            })
            if (obj) {
                return obj.value
            }
        },
        //  获取用户列表(销售人员)
        getUserList() { // key = '1'
            let path = this.getPathByKey('1')
            getUserListByMenu({menuUrl: path}).then(response => {
              if(response.data.code == '0'){
                this.userOption = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
        },
      printInfo() {
        let params = filterParams(this.dataForm)
        getOptometryListExport(params).then(response => {
          var blob = new Blob([response.data], {
            type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
          });
          var reader = new FileReader();
          reader.readAsDataURL(blob);
          reader.onload = function(e) {
            var a = document.createElement('a');
            a.download = "屈光档案检查信息.xlsx";
            a.href = URL.createObjectURL(blob);
            a.click();
          }
          this.$message.success("导出成功")
        })
      },
    }
  }
</script>
